package com.lute.utils;

import java.util.List;

import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;

import com.lute.model.User;


public class LdapDAO implements LdapInterfaceDAO {

	@Autowired
	private LdapTemplate ldapTemplate;

	public LdapDAO(){}
	 
    private static class UserAttributesMapper implements AttributesMapper {
	
    	public Object mapFromAttributes(Attributes attrs) throws javax.naming.NamingException {
	
            User user = new User();
            if (attrs.get("uid") != null) {
                user.setLogin((String) attrs.get("uid").get());
            }
            if (attrs.get("cn") != null) {
            	String tmp = (String)attrs.get("cn").get();
            	String []tmpTab = tmp.split(" ");
	            user.setFirst_name(tmpTab[0]);
            }
            if (attrs.get("sn") != null) {
                user.setLast_name((String) attrs.get("sn").get());
            }
            if (attrs.get("mail") != null) {
                user.setEmail((String) attrs.get("mail").get());
            }
            return user;
	     }
	}
	
	public boolean doAuthenticate(String uid, String password) {
		AndFilter filter = new AndFilter();
		filter.and(new EqualsFilter("objectclass", "person")).and(
				new EqualsFilter("uid", uid));
		boolean authentifie = ldapTemplate.authenticate(
				"ou=users,o=lute", filter.toString(), password);
		return authentifie;
	}

	public User findUser(final String userName) {
		AndFilter filter = new AndFilter();
		filter.and(new EqualsFilter("objectclass", "person")).and(	  
		new EqualsFilter("uid", userName));
		List<User> users = ldapTemplate.search("ou=users,o=lute", 
		filter.encode(), new UserAttributesMapper());
		if (!users.isEmpty()) {
			return users.get(0);
		}  
	    return null;  
	  }
	 public void modifyAttribute(String commonName,String newValue){
	        Attribute loginFailed = new BasicAttribute("l", newValue);
	        ModificationItem loginFailure = new ModificationItem(
	            DirContext.REPLACE_ATTRIBUTE, loginFailed);
	        ldapTemplate.modifyAttributes("cn="+commonName+",ou=users,o=lute", new ModificationItem[]{loginFailure});
	    }
	public String getFailureNumber(String commonName){
		String[] attrs = {"l"};
		String val = (String)ldapTemplate.lookup("cn="+commonName+",ou=users,o=lute", attrs,
			   new AttributesMapper() {
				@Override
				public Object mapFromAttributes(Attributes arg0) throws javax.naming.NamingException {
					if(arg0.get("l") == null){
						return false;
					}
					return (String)arg0.get("l").get();	
				}
			});
		return val;
	}
}